package com.woniuxy.qiantai.controller;


import com.woniuxy.qiantai.service.UserService;
import com.woniuxy.qiantai.utils.CookieUtils;
import com.woniuxy.qiantai.utils.JwtUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author firstGroup
 * @since 2023-02-22 09:45:58
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @PostMapping("reg")
    public String reg(String username, String password, String repass, String email, String emailCode, String tel) {


        String emailCodeRedis = stringRedisTemplate.opsForValue().get(email);


        if (StringUtils.isEmpty(emailCodeRedis) || !emailCodeRedis.equals(emailCode)) {

            return "验证码错误";
        }


        try {
                userService.reg(username, password, repass, email, tel);

        } catch (RuntimeException exception) {

            exception.printStackTrace();  //打印异常栈

            return exception.getMessage();  //返回异常信息
        }

        return "ok";

    }

    @RequestMapping("login")
    @ResponseBody
    public String login(String username, String password, HttpServletResponse response) {


        try {
            userService.loginCheck(username, password);


            String token = JwtUtils.createToken(username, 15);
            stringRedisTemplate.opsForValue().set(token, username, 30, TimeUnit.MINUTES);
            CookieUtils.setUserToken2Cookie(response, token);


        } catch (RuntimeException exception) {
            exception.printStackTrace();

            return exception.getMessage();
        }

        return "ok";
    }

    @RequestMapping("currentUserName")
    @ResponseBody
    public String currentUserName(HttpServletRequest request) {

        String userTokenFromCookie = CookieUtils.getUserTokenFromCookie(request);
        //然后从userToken中解析出用户名
        String accountFromToken = JwtUtils.getAccountWithoutException(userTokenFromCookie);
        return accountFromToken;

    }

    @RequestMapping("logout")
    @ResponseBody
    public String logout(HttpServletResponse response,HttpServletRequest request) {
        CookieUtils.deleteUserTokenFromCookie(response);
        String token = CookieUtils.getUserTokenFromCookie(request);
        stringRedisTemplate.delete(token);
        return "ok";
    }

    @RequestMapping(value = "getEmailCode", method = RequestMethod.POST)
    @ResponseBody
    public String sendEmail(String email, HttpServletRequest request) {
        //正则表达式校验邮箱
        String regex = "^(.+)@(.+)$";
        Pattern pattern = Pattern.compile(regex);
        boolean isOK = pattern.matcher(email).matches();
        if (!isOK) {
            return "请输入正确邮箱";
        }
        rabbitTemplate.convertAndSend("sendEmailExchange", "sendEmail", email);
        System.out.println(email);
        return "ok";
    }

    @RequestMapping("test")
    public String test(String email) {
        return "";
    }
}
